/*
 * Famon (http://code.google.com/p/famon/)
 * Copyright (c) 2010 Carl Crous.
 *
 * This file is part of Famon.
 *
 * Famon is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Famon is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with Famon.  If not, see <http://www.gnu.org/licenses/>.
 */

package za.org.foobar.famon.collector;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import za.org.foobar.famon.webapp.UserLogic;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


/**
 * @author Carl Crous
 */
public class FamonQueueProcessorServlet extends HttpServlet {
    private final Log log = LogFactory.getLog(getClass());
    private FamonCollectorLogic famonCollectorLogic;

    public FamonQueueProcessorServlet() {
        log.debug("Creating FamonQueueProcessorServlet");
    }

    public void service(HttpServletRequest request, HttpServletResponse servletResponse) throws ServletException, IOException {
        long t = System.currentTimeMillis();
        try {
            UserLogic.get().setSystemUser(true);
            FamonCollectorLogic.get().processQueue(servletResponse.getOutputStream());
        } finally {
            UserLogic.get().setSystemUser(false);
        }
        t = System.currentTimeMillis() - t;
        log.info("Queue took " + t + " ms to process");
    }
}
